<?php

namespace App\Http\Controllers\Foundation;


use Illuminate\Http\Request;
use App\Model\System\Token;
use Laravel\Lumen\Routing\Controller as BaseController;


use App\Model\Foundation\TimeStock;
use App\Http\Controllers\MessagePacket;


/**
 * 处理教师可用时间
 * Class TimeStockController
 * @package App\Http\Controllers\Foundation
 */
class TimeStockController extends BaseController
{
    use MessagePacket;


    function  __construct(){}


    /**
     * 获取单一可用时间的信息
     * @param $id
     * @return packetMessage
     */
    function getTimeStock($id)
    {
        $id = intval($id);
        $timeStock = TimeStock::find($id);
        if(empty($timeStock)){
            return $this->fail($timeStock, '数据不存在');
        }
        return $this->success($timeStock);
    }


    /**
     * 批量查询可用时间
     * @param Request $request
     * @return packetMessage
     */
    function getTimeStocks(Request $request)
    {
        $tuid = 0;
        if($request->has('teacheruid')) {
            $tuid = intval($request->get('teacheruid'));
        }
        $timeStock = TimeStock::where('teacheruid', $tuid);
        if($request->has('begintime')){
            $timeStock->where('begintime', '>=', $request->get('begintime'));
        }
        if($request->has('endtime')){
            $timeStock->where('endtime', '<', $request->get('endtime'));
        }
        # 需要／不需要 分页
        if($request->has('page')) {
            $limit = 30;
            if ($request->has('perPage')) {
                $limit = $request->get('perPage');
            }
            $timeStocks = $timeStock->paginate($limit)->items();
        }else{
            $timeStocks = $timeStock->get();
        }

        if(empty($timeStocks)){
            return $this->fail($timeStocks, '数据不存在');
        }
        return $this->success($timeStocks);
    }


    /**
     * 添加一个可用时间
     * @param Request $request
     * @return packetMessage
     */
    function  addTimeStock(Request $request)
    {
        $timeStock = new TimeStock($request->all());
        $timeStock->created_by = Token::user()->userid;
        $timeStock->updated_by = Token::user()->userid;
        if($timeStock->save()){
            return $this->success($timeStock);
        }else{
            return $this->fail(false,'保存失败');
        }
    }


    /**
     * 更新某个可用时间的信息
     * @param Request $request
     * @return packetMessage
     */
    function  updateTimeStock($id=null, Request $request)
    {
        # 兼容旧模式
        if(!$id){
            $id = $request->get('timeuid');
        }

        $timeStock = TimeStock::find($id);
        $timeStock->update($request->all());
        $timeStock->updated_by = Token::user()->userid;

        if($timeStock->save()){
            return $this->success($timeStock);
        }else{
            return $this->fail(false,'保存失败');
        }
    }


    /**
     * 删除某个时间
     * @param $id
     * @return packetMessage
     */
    function  deleteTimeStock($id)
    {
        if(TimeStock::destroy($id)){
            return $this->success(true);
        }else{
            return $this->fail(false,'删除成功');
        }

    }

}
